package com.yl.chainStore.control.warehouse

import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.yl.chainStore.common.enums.Status
import com.yl.chainStore.control.BaseCtrl
import com.yl.chainStore.entity.warehouse.WarehouseDispatchOrder
import com.yl.chainStore.pojo.vo.warehouse.WarehouseDispatchOrderSelectVo
import com.yl.chainStore.services.warehouse.IWarehouseDispatchOrderService
import com.yl.chainStore.services.warehouse.IWarehouseService
import com.yl.chainStore.services.IWebCompanyUserService
import com.yl.common.ErrorCode
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnError
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import com.yl.users.common.annotations.PermsCode
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.interceptor.TransactionAspectSupport
import org.springframework.web.bind.annotation.*

/**
 * 调度单操作
 */
@RestController
@RequestMapping("warehouse/dispatchOrder")
open class WarehouseDispatchOrderController(
	private val dispatchOrderService: IWarehouseDispatchOrderService,
	private val iWarehouseService: IWarehouseService,
	private val iWebCompanyUserService: IWebCompanyUserService
):BaseCtrl() {

    /**
     * 调度单列表
     */
    @PostMapping("dispatchOrderPage")
    @PermsCode("warehouse:dispatchOrder:dispatchOrderPage")
    @Transactional
    open fun dispatchOrderPage(@RequestBody vo: WarehouseDispatchOrderSelectVo): ResponseBody {
        vo.deptId = getContext().subCompanyId
        val page = Page<WarehouseDispatchOrder>(vo.current, vo.size )
        return returnSuccess(
            iWarehouseService.dispatchOrderPage(page, vo)
        )
    }

    /**
     * 调度单明细
     */
    @GetMapping("dispatchOrderDetail/{id}")
    @PermsCode("warehouse:dispatchOrder:dispatchOrderDetail")
    @Transactional
    open fun dispatchOrderDetail(@PathVariable id: Long): ResponseBody {
        val dispatchOrder = dispatchOrderService.dispatchOrder(id)
        if(dispatchOrder != null){
            dispatchOrder.details = dispatchOrderService.getOrderDetails(id)
        }
        return returnSuccess(
            dispatchOrder
        )
    }

    /**
     * 调度单暂存
     */
    @PostMapping("staging")
    @Transactional(rollbackFor = [Exception::class])
    open fun staging(@RequestBody dispatchOrder : WarehouseDispatchOrder): ResponseBody {
        if(dispatchOrder.id != 0L){
            dispatchOrderService.deleteDispatchOrder(dispatchOrder.id)
        }


        val addDispatchOrder = iWebCompanyUserService.stagingDispatchOrder(dispatchOrder)

        if (addDispatchOrder.code != ErrorCode.SUCCESS.code) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
            return addDispatchOrder
        }
        var i = 1
        for(detail in dispatchOrder.details!!){
            detail.inStatus = Status.STAGING
            detail.outStatus = Status.STAGING
            detail.dispatchOrderId = dispatchOrder.id
            val addDetail = dispatchOrderService.addDetail(detail)
            if (addDetail.code != ErrorCode.SUCCESS.code) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
                return returnError(ErrorCode.DATA_SAVE_FAILED,"第"+i+"行物品错误："+addDetail.msg)
            }
            i++
        }
        return returnSuccess()
    }

    /**
     * 获取暂存调度单
     */
    @GetMapping("getStaging")
    @Transactional
    open fun getStaging():ResponseBody{
        val order = iWebCompanyUserService.getStagingDispatchOrder(getContext().userId, getContext().subCompanyId)
        if(order != null){
            order.details = dispatchOrderService.getOrderDetails(order.id)
        }
        return returnSuccess(order)
    }

    /**
     * 调度单申请
     */
    @PostMapping("addDispatchOrder")
    @PermsCode("warehouse:dispatchOrder:addDispatchOrder")
    @Transactional(rollbackFor = [Exception::class])
    open fun addDispatchOrder(@RequestBody dispatchOrder : WarehouseDispatchOrder): ResponseBody {
        if(dispatchOrder.id != 0L){
            dispatchOrderService.deleteDispatchOrder(dispatchOrder.id)
        }

        val addDispatchOrder = iWarehouseService.addDispatchOrder(dispatchOrder)

        if (addDispatchOrder.code != ErrorCode.SUCCESS.code) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
            return addDispatchOrder
        }
        var i = 1
        for(detail in dispatchOrder.details!!){
            detail.inStatus = Status.WAIT_AUDIT
            detail.outStatus = Status.WAIT_AUDIT
            detail.dispatchOrderId = dispatchOrder.id
            val addDetail = dispatchOrderService.addDetail(detail)
            if (addDetail.code != ErrorCode.SUCCESS.code) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
                return returnError(ErrorCode.DATA_SAVE_FAILED,"第"+i+"行物品错误："+addDetail.msg)
            }
            i++
        }
        return returnSuccess()
    }

    /**
     * 调度单全部确认
     */
    @PostMapping("completeDispatchOrder/{id}")
    @Transactional(rollbackFor = [Exception::class])
    @PermsCode("warehouse:dispatchOrder:audit")
    open fun completeDispatchOrder(@PathVariable id: Long): ResponseBody {
        val addInOrder = dispatchOrderService.completeDispatchOrder(id)
        if (addInOrder.code != ErrorCode.SUCCESS.code) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
        }
        return addInOrder
    }

    /**
     * 调度单审核失败
     */
    @PostMapping("noPassDispatchOrder/{id}")
    @Transactional(rollbackFor = [Exception::class])
    @PermsCode("warehouse:dispatchOrder:audit")
    open fun noPassDispatchOrder(@PathVariable id: Long): ResponseBody {
        val addInOrder = dispatchOrderService.noPassDispatchOrder(id)
        if (addInOrder.code != ErrorCode.SUCCESS.code) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
        }
        return addInOrder
    }



    /**
     * 调度单全部取消
     */
    @PostMapping("cancelDispatchOrder/{id}")
    @Transactional(rollbackFor = [Exception::class])
    @PermsCode("warehouse:dispatchOrder:cancelDispatchOrder")
    open fun cancelDispatchOrder(@PathVariable id: Long): ResponseBody {
        val addInOrder = dispatchOrderService.cancelDispatchOrder(id)
        if (addInOrder.code != ErrorCode.SUCCESS.code) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
        }
        return addInOrder
    }



}
